<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/7  11:32
 */
namespace app\admin\controller;
use think\Db;

class Withdraw extends Common
{
    private $table_admin = "";
    /*
   *	构造函数
   */
    function __construct()
    {
        parent::__construct();
        $this->table_admin=db('withdraw');
    }

    public function index()
    {
        $wd_result = $this->table_admin
            ->order('id desc')
            ->paginate(15)->each(function($item,$key){
                $item['user'] = db('member')->where('id='.$item['user_id'])->value('nickname');
                $member_buyer_check = db('member_buyer_check')->where(['user_id'=>$item['user_id']])->field('mobile,id_card')->find();
                $item['money'] = $item['money']/100;
                return $item;
            });
//        halt($wd_result);
        $this->assign('wd_result', $wd_result);
        $this->assign('page', $wd_result->render());
        return $this->fetch();
    }

    public function withdrawCheck()
    {

        $db_withdraw = db('withdraw');
        $id = input('id/d');
        if(request()->isAjax()){
            $id = input('id/d');
            $status = input('status/d');
            $withdraw = $db_withdraw->where("id='$id'")->find();
            if($status=='0'){
                $this->error('审核状态有误');
            }

            $platform_amount_count = platform_amount_count();

            if($withdraw['money'] > $platform_amount_count['platform_amount_total'])$this->error('平台没有钱提现!');

            $data['status']=$status;
            $data['display']=1;
            $data['check_time'] = time();
            $res = $db_withdraw->where("id='$id'")->update($data);

            $platform['type'] = -1;
            $platform['type_name'] = 2;
            $platform['price'] = $withdraw['money'];
            $platform['wid'] = $withdraw['id'];
            $res_platform = db('platform_amount')->insert($platform);

            if(!$res || !$res_platform) $this->error('系统繁忙，稍后再试');
            return $this->success('审核成功',url('Withdraw/index'));
        }
        $da = $db_withdraw->where('id='.$id)->find();
        $member = db('member')->where(['id'=>$da['user_id']])->field('wx_openid,nickname')->find();
        $member_buyer_check = db('member_buyer_check')->where(['user_id'=>$da['user_id']])->field('mobile,id_card')->find();
        $this->assign('da', $da);
        $this->assign('member', $member);
        $this->assign('member_buyer_check', $member_buyer_check);
        return $this->fetch('withdraw/checkWithdraw');
    }

}